package com.team2.shoppingService;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.team2.shared.*;

public class BestBuyShoppingRequest extends ShoppingRequest {
	private String apiKey = "8ud2s67xbycgdx2pb8apaygc";
	private String baseUrl = "http://api.remix.bestbuy.com/v1/";
	private String longitude = "-77.03";
	private String latitude = "38.89";
	private String radius = "10";
	private boolean isStoreInfo = false;
	private String filterString = "";
	private int pageNumber = 1;
	private int pageSize = 10;
	public BestBuyShoppingRequest(String tags) {
		this.tags = tags;
		this.filterString = "inStoreAvailability=true";
		// TODO Auto-generated constructor stub
	}
	public String toUrlString(){
		String url= baseUrl;
		url += "products("+filterString+"&search="+tags.replace(" ", "+")+")";//fixme replace is a hack
		url += "+stores(area("+latitude+","+longitude+","+radius+"))?";
		url += "apiKey="+ this.apiKey+"&format=json";
		url += "&sort=regularPrice.asc";
		url += "&page="+Integer.toString(pageNumber);
		url += "&show=name,salePrice,shortDescription,longDescription,image,manufacturer,stores";
		return url;
	}
	
	/**
	 * 
	 * @param keyValue a condition filter used for querying
	 *                 in the following format: key=value
	 *                 Example: color=black ; customerReviewAverage>3.5
	 */
	public void addFilter(String keyValue){
		if(filterString == null || filterString.equals("")){
			filterString = keyValue;
		}
		else {
			filterString += "&"+keyValue;
		}
	}

	
	@Override
	public ArrayList<Product> execute() {
		String json = Util.getRawHttpResponse(toUrlString());
		
		return this.parseWithStoreInfo(json);
	}

	public String getLongitude() {
		return longitude;
	}

	public void setLongitude(float longitude) {
		this.longitude = Float.toString(longitude);
	}

	public String getLatitude() {
		return latitude;
	}

	public void setLatitude(float latitude) {
		this.latitude = Float.toString(latitude);
	}

	public String getRadius() {
		return radius;
	}

	/**
	 * 
	 * @param radius searching range for local stores in unit of mile
	 */
	public void setRadius(int radius) {
		this.radius = Integer.toString(radius);
	}

	public boolean isStoreInfo() {
		return isStoreInfo;
	}

	public void setStoreInfo(boolean isStoreInfo) {
		this.isStoreInfo = isStoreInfo;
	}

	public String getFilterString() {
		return filterString;
	}

	public void setFilterString(String filterString) {
		this.filterString = filterString;
	}
	private ArrayList<Product> parseWithStoreInfo(String json){
		ArrayList<Product> products = new ArrayList<Product> ();
		try {
			JSONObject root = new JSONObject(json);
			JSONArray productList = root.getJSONArray("products");
			for(int i = 0; i<productList.length(); i++){
				Product product = new Product();
				JSONObject object = productList.getJSONObject(i);
				
				//parse the product information
				product.setTitle(object.getString("name"));
				product.setDescription(object.getString("shortDescription"));
				product.setImageLink(object.getString("image"));
				product.setBrand(object.getString("manufacturer"));
				String price = Double.toString(object.getDouble("salePrice"));
				
				//parse the store information and store the price
				JSONArray storeArray = object.getJSONArray("stores");
				ArrayList<Store> stores = new ArrayList<Store>();
				for(int j=0;j<storeArray.length();j++){
					JSONObject storeObject = storeArray.getJSONObject(j);
					Store store = new Store();
					store.setAddress(storeObject.getString("address"));
					store.setName(storeObject.getString("longName"));
					store.setId(Integer.toString(storeObject.getInt("storeId")));
					store.setHours(storeObject.getString("hours"));
					store.setCity(storeObject.getString("city"));
					store.setPostalCode(storeObject.getString("postalCode"));
					store.setPrice(price);
					store.setBrand("BestBuy");
					stores.add(store);
				}
				
				//add the product to returned list
				product.setStores(stores);
				products.add(product);
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return products;
	}
	public int getPageNumber() {
		return pageNumber;
	}
	public void setPageNumber(int pageNumber) {
		this.pageNumber = pageNumber;
	}
}
